var util = require('../../utils/util.js')
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var reload = true;
var page = 2;
var hasMore = true;
var loadingMore = false;

var SDKVersion = '';
var status = true;

var jujueShow = false;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgDomain: app.globalData.imgDomain,

    classlysData: '',

    zxsData: [],
    hasData: true,
    util: util,
    uid: 0,
    token: '',
    openid: '',
    canEdit: 0,

    userinfo: {},

    bottom_nav_data: [],
    isIphone_x: false,
    nav_bgColor: '#ffffff',

    shouqian_phone: '',
    colorSet: '',
    wz_item_img_w_1: 300,

    yewuxinxi:[],

    jujueArray: ["放假", "缺少师傅", "时间冲突"],
    jujueIndex: 0,
    jujueid:'',
    jujuestate:'',

    group:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.type);
    var group = options.group;
    var that = this;
    var tc = app.get_tc();
    that.setData({
      tc: tc,
      group: group,
    })
    var titlename = tc == 1 ? '信息管理' : '信息管理';
    wx.setNavigationBarTitle({
      title: titlename,
    });

    page = 2;
    hasMore = true;
    loadingMore = false;

    var that = this;

    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color,
      })
      that.resetNavColor();
    });

    //初始化底部导航
    var b_nav_data = app.getBottomNavData('car-commerce/car-commerce', function (data) {
      that.setData({
        bottom_nav_data: data,
        nav_bgColor: data[0].bgcolor,
      });
    });

    wx.getSystemInfo({
      success: function (res) {
        SDKVersion = res.SDKVersion;
        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        that.setData({
          tc: app.get_tc(),
          isIphone_x: isIphone_x,
          wz_item_img_w_1: res.windowWidth - 25
        });
      }
    })
    that.setData({
      windowWidth: wx.getSystemInfoSync().windowWidth,
      windowHeight: wx.getSystemInfoSync().windowHeight
    }) 

    reload = true;
    //当前登录用户信息
    app.LoginUserInfo(function (res) {
      var authFail = res == -1;//用户拒绝授权
      if (!authFail) {
        //更新数据
        that.setData({
          uid: res.uid,
          token: res.token,
          openid: res.wxsmall_openid,
        });
      }else {
        app.reAuthorize(reGetUserInfo);
      }

      util.api({
        data: {
          "uid": res.uid,
          "publickey": util.publiccode,
          "ac": "userinfo"
        },
        success: function (responseData) {
          console.log(responseData.data);
          that.setData({
            userinfo: responseData.data
          });
        }
      });

    });
    //获取关于等信息
    app.getAllAbout('shouqian', function (res) {
      that.setData({
        shouqian_phone: res
      })
    });

    // that.getLiuyan();
    // 判断管理员查询全面信息
    // if (that.data.uid == '15896' || that.data.uid == '15961' ||that.data.uid == '15976' || that.data.uid == '16461'){
    //   that.getWaithao();
    // }else{
      that.getMyhao(); 
    // }
     
  },


  //获取留言信息
  // getLiuyan: function () {
  //   var that = this;
  //   var params = {
  //     publickey: util.publiccode,
  //     ac: "getliuyan",
  //     uid: that.data.uid,
  //     orgid: util.orgid,
  //   }
  //   util.api({
  //     data: params,
  //     success: function (responseData) {
  //       console.log("留言数据：", responseData.data);

  //       var result = responseData.data;
  //       for (var i = 0; i < result.length; i++) {

  //         result[i]['post_time'] = that.toDate(result[i]['post_time']);

  //       }
  //       that.setData({
  //         liuyanArray: result
  //       })
  //     }
  //   });
  // },

   //查询预约信息
  getWaithao:function(){
    var that = this;

    var privatekey = utilMd5.hexMD5(that.data.uid + 'waithao' + that.data.token);
    console.log("that.data.uid:"+that.data.uid+"that.data.token:"+that.data.token+"util.orgid:"+util.orgid);
     util.api({
      method: "post",
      data: {
        ac: "waithao",
        uid: that.data.uid,
        orgid: util.orgid, 
        privatekey: privatekey
      },
      
      success: function (responseData) {
        console.log("调试--查询领号等待信息接口:", responseData.data);
        var resultData = responseData.data;
        for (var i = 0; i < resultData.length; i++) {

          resultData[i]['ctime'] = that.toDate(resultData[i]['ctime']);

        }
        that.setData({
          yewuxinxi: resultData,
        });
        
      }
    });
  },


  //我的排号信息
  getMyhao:function(){
    
    var that = this;
     util.api({
      method: "get",
      data: {
        ac: "getmianshi",
        uid: that.data.uid,
        orgid: util.orgid,
        group: that.data.group,
     
      },
      
      success: function (responseData) {
        console.log("调试--我的排号信息接口:", responseData.data);
        console.log("调试--我的排号号码:", responseData.data.number);
        var resultData = responseData.data;
        for (var i = 0; i < resultData.length; i++) {

          resultData[i]['ctime'] = that.toDate(resultData[i]['ctime']);

        }
        that.setData({
          myhao:resultData
        })
      }
    });
  },

  huifang: function (e) {
    console.log(e);
    var id = e.currentTarget.dataset.id;
    var state = e.currentTarget.dataset.state;
    var selector = e.detail.value;
    var that = this;
    var privatekey = utilMd5.hexMD5(that.data.uid + 'edityuyue' + that.data.token);
    util.api({
      method: "POST",
      data: {
        ac: "edityuyue",
        uid: that.data.uid,
        id: id,
        state: state,
        orgid: util.orgid,
        jujueliyou:0,
        privatekey: privatekey
      },
      success: function (responseData) {
        console.log("接口返回信息",responseData);
        if (responseData.data.res == true) {
          // wx.showToast({
          //   title: '提交成功',
          //   icon: 'success',
          //   duration: 3000,
          // })
          var contentname = this.data.tc == 1 ? '提交成功' : '提交成功';
          app.dialogToast(contentname, "success", function () {
            wx.reLaunch({ url: '/pages/mine/mine' });
          })
        } else {
          wx.showToast({
            title: '提交失败',
            icon: 'success',
            duration: 3000,
          })
        }

      }
    });
  },

  huifang2: function (e) {
    console.log(e);
    var that = this;

    var id = that.data.jujueid;
    var state = that.data.jujuestate;
    // var selector = e.detail.value.selector;
    var message = e.detail.value.desc;
    var that = this;
    var privatekey = utilMd5.hexMD5(that.data.uid + 'editmianshi' + that.data.token);
    util.api({
      method: "POST",
      data: {
        ac: "editmianshi",
        uid: that.data.uid,
        id: id,
        state: state,
        orgid: util.orgid,
        // jujueliyou: selector,
        privatekey: privatekey,
        message: message
      },
      success: function (responseData) {
        console.log("接口返回信息", responseData);
        if (responseData.data.res == true) {
          var contentname = this.data.tc == 1 ? '提交成功' : '提交成功';
          app.dialogToast(contentname, "success", function () {
            wx.reLaunch({ url: '/pages/mine/mine' });
          })
        } else {
          wx.showToast({
            title: '提交失败',
            icon: 'success',
            duration: 3000,
          })
        }

      }
    });
  },


  //点击图片放大
  showEWM: function (e) {
    var shareImgUrl = e.currentTarget.dataset.imgsrc;
    wx.previewImage({
      current: shareImgUrl,
      urls: [shareImgUrl]
    })
  },




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


  goAtherPage: function (e) {
    var url = e.currentTarget.dataset.link;
    wx.navigateTo({ url: url })
  },
  goAtherPageRedirect: function (e) {
    var url = e.currentTarget.dataset.link;
    wx.redirectTo({ url: url })
  },

  //保存formId
  submitFormId: function (e) {
    app.saveFormId(e.detail.formId);

    var path = e.detail.value.nav_path;
    if (path) {
      wx.redirectTo({ url: path })
    }
  },

  resetNavColor: function () {
    var bg_color = '#d8202d';
    var fg_color = '#ffffff';
    if (this.data.colorSet) {
      var colorJO = this.data.colorSet;
      bg_color = colorJO.titlebg;
      fg_color = colorJO.titlefont;
    }
    wx.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    })
  },

  //时间戳转换时间  
  toDate: function (number) {
    var n = number * 1000;
    var date = new Date(n);
    var Y = date.getFullYear() + '.';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '.';
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    var H = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
    var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
    return (Y + M + D + "  " + H + ':' + m)
  },

  makeCallPhone: function (e) {
    // app.saveFormId(e.detail.formId);
    var phoneNum = e.currentTarget.dataset.number;
    console.log("phoneNum:" + phoneNum);
    wx.makePhoneCall({
      phoneNumber: phoneNum,
      success: function (e) {
        console.log("make call success:", JSON.stringify(e));
      },
      fail: function (e) {
        console.log("make call fail:", JSON.stringify(e));
      }
    })
  },

  //点击拒绝
  jujue: function (e) {
    var id = e.currentTarget.dataset.id;
    var state = e.currentTarget.dataset.state;
    console.log(id);
    var that = this;
    that.setData({
      jujueShow: true,
      jujueid: id,
      jujuestate: state
    })
  },

  //作品类型选项改变值
  jujuePickerChange: function (e) {//选项改变触发事件
    console.log("选中了:", e);
    this.setData({
      jujueIndex: e.detail.value
    })
  },

  //隐藏拒绝列表
  hidejujue: function () {

    this.setData({
      jujueShow: false
    })
  },

})